<template>
	<view class="container page-backgrund">
		<view class="logo">
			<image class="pic" src="https://peixin.peiyu.online/xmk/images/新膜客.png" mode="widthFix"></image>
		</view>
		<button class="login-button" @click="register" type="default" :open-type="isAgree?'getPhoneNumber':''"
			@getphonenumber="getPhoneNumber">
			<image class="pic" src="https://peixin.peiyu.online/xmk/images/微信.svg" mode="aspectFill"></image>
			<text class="txt">微信一键登录</text>
		</button>
		<view class="section">
			<view class="protocal-agree">
				<uni-icons :type="isAgree?'checkbox':'circle'" :color="isAgree?'#4dea00':'#9e9e9e'" size="26"
					@click="change_isAgree"></uni-icons>
				<text class="txt1" :class="isAgree?'txt1-1':'txt1'">同意</text>
				<text class="txt2">《用户协议和隐私协议》</text>
			</view>
			<u-divider text="员工身份登录" :hairline="true" textColor="#dcdcdc"></u-divider>
			<view class="charactor-choose">
				<uni-icons :type="isEmployee?'checkbox':'circle'" :color="isEmployee?'#4dea00':'#9e9e9e'" size="26"
					@click="change_charactor"></uni-icons>
				<text class="txt1" :class="isEmployee?'txt1-1':'txt1'">我是员工</text>
				<text class="txt1" v-if="!isEmployee">(用户请勿选择)</text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		api_employee_login,
		api_custom_login
	} from "../../apis/apis.js";
	import {
		useMainStore
	} from '@/utils/store.js';

	const store = useMainStore();
	const isAgree = ref(false);
	const isEmployee = ref(false);
	const wxCode = ref("");
	const phoneCode = ref("");

	//点击勾选协议
	const change_isAgree = () => {
		isAgree.value = !isAgree.value;
	};

	//点击我是员工
	const change_charactor = () => {
		isEmployee.value = !isEmployee.value;
	};

	// 点击获取微信临时证明code
	const register = async () => {
		if (!isAgree.value) {
			uni.showToast({
				icon: "none",
				title: "请勾选同意用户协议和隐私协议"
			});
			return;
		}
		try {
			const profileRes = await uni.getUserProfile({
				desc: '获取微信信息',
				lang: 'zh_CN'
			});
			console.log('用户信息', profileRes);

			const loginRes = await uni.login({
				provider: 'weixin'
			});
			wxCode.value = loginRes.code;
			console.log('wx code', wxCode.value);
		} catch (error) {
			console.error("错误:", error);
		}
	};

	// 获取phone code
	const getPhoneNumber = async (e) => {
		if (!wxCode.value) {
			return;
		}
		phoneCode.value = e.detail.code;
		if (phoneCode.value) {
			console.log("phone code", phoneCode.value);
		} else {
			uni.showToast({
				icon: "none",
				title: "操作取消"
			})
		}
		//登录
		handleLogin();
	};

	// 登录
	const handleLogin = async () => {
		let params = {
			"code": wxCode.value,
			"phoneCode": phoneCode.value
		}
		if (isEmployee.value) {
			const res = await api_employee_login(params)
			if (res.data.jobId == 2) {
				store.set_currentUser(res.data);
				uni.reLaunch({
					url: "/pages/salesman/index/index",
				});
			} else if (res.data.jobId == 3) {
				store.set_currentUser(res.data);
				uni.reLaunch({
					url: "/pages/manager/index/index",
				});
			} else if (res.data.jobId == 4) {
				store.set_currentUser(res.data);
				uni.reLaunch({
					url: "/pages/worker/index/index",
				});
			} else if (res.data.jobId == 5) {
				store.set_currentUser(res.data);
				uni.reLaunch({
					url: "/pages/storeman/index/index",
				});
			} else if (res.data.jobId == 6) {
				store.set_currentUser(res.data);
				uni.reLaunch({
					url: "/pages/financialman/index/index",
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		width: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.logo {
			width: 417rpx;
			margin: 186rpx 0 506rpx 0;

			.pic {
				width: 417rpx;
			}
		}

		.login-button {
			//登录按钮
			display: flex;
			justify-content: center;
			align-items: center;
			width: 650rpx;
			height: 94rpx;
			border-radius: 10rpx;
			border: 3rpx solid $primary-color;
			margin-bottom: 42rpx;
			background-color: rgba(0, 0, 0, 0);

			.pic {
				width: 50rpx;
				height: 50rpx;
				padding-right: 10rpx;
			}

			.txt {
				height: 38rpx;
				font-family: Alibaba PuHuiTi 2.0 light;
				font-size: 38rpx;
				color: $primary-color;
				line-height: 38rpx;
				text-align: left;
			}
		}

		.section {
			display: flex;
			flex-direction: column;
			gap: 34rpx;

			.protocal-agree {
				display: flex;
				justify-content: left;
				align-items: center;

				.txt1 {
					margin-left: 5rpx;
					font-family: Alibaba PuHuiTi 2.0 light;
					color: $font-sub-color;
					text-align: left;
					font-size: 30rpx;
				}

				.txt1-1 {
					color: $primary-color;
				}

				.txt2 {
					font-family: Alibaba PuHuiTi 2.0 light;
					color: $primary-color;
					text-align: left;
					font-size: 30rpx;
				}
			}

			.charactor-choose {
				display: flex;
				justify-content: left;
				align-items: center;

				.txt1 {
					margin-left: 5rpx;
					font-family: Alibaba PuHuiTi 2.0 light;
					color: $font-9e-color;
					text-align: left;
					font-size: 30rpx;
				}

				.txt1-1 {
					color: $primary-color;
				}
			}
		}
	}
</style>